import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 读取图像
img = cv.imread("image/wulin.jpeg")
temp = cv.imread("image/bai.jpeg")
plt.imshow(img[:, :, ::-1])
plt.show()
plt.imshow(temp[:, :, ::-1])
plt.show()

# 进行模板匹配(可变性不强，实用性不强，以后会有更好的算法代替，这里看个热闹)
res = cv.matchTemplate(img, temp, cv.TM_CCORR)
plt.imshow(res, cmap=plt.cm.gray)
plt.show()

min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
top_left = max_loc
h, w = temp.shape[:2]
bottom_right = (top_left[0] + w, top_left[1] + h)
cv.rectangle(img, top_left, bottom_right, (0, 255, 255), 2)
plt.imshow(img[:, :, ::-1])
plt.show()